<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use App\Models\User;
use App\Models\UserProduct;

use App\Models\UserProductEvent;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserProductController extends Controller
{
    use ModelForm;
    protected $header = "用户设备管理";
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        
        return Admin::content(function (content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserProduct::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->tab("设备基础信息",function()use($form){
                $form->display('user_id', '用户ID');
                $form->select('status', "安装状态")->options(function ($id) {
                    $map = [];
                    $map[UserProduct::STATUS_WAIT_INSTALL] = '未安装';
                    $map[UserProduct::STATUS_INSTALL] = '已安装';
                    return $map;
                });
                $form->datetime('last_change_time', '上次更换滤芯时间');
                $form->datetime('next_change_time', '下次更换滤芯时间');
                $form->datetime('end_time', '合同到期时间');
                $form->datetime('continue_time', '下次续费时间');
                $form->datetime('installed_at', '设备安装时间');
                $form->display('created_at', '创建时间');
            });
            $form->tab("设备维护记录",function()use($form){
                $form->hasMany('event',"设备维护记录", function (Form\NestedForm $form) {
                    $form->select('type', "类型")->options(function ($id) {
                        $map = [];
                        $map[UserProductEvent::EVENT_FILTER] = '换滤芯';
                        $map[UserProductEvent::EVENT_FIX] = '修理';
                        return $map;
                    });
                    $form->text('title', "标题");
                    $form->text('content', "内容")->attribute(['width'=>"100","height"=>"60"]);
                    $form->datetime('created_at','发生时间');
                });
            });

//            $form->display('updated_at', '更新时间');
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserProduct::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->id('ID')->sortable();
            $grid->column('', '用户手机')->display(function(){
                return User::find($this->user_id)->mobile;
            });
            $grid->status('状态')->display(function(){
                return  $this->status == UserProduct::STATUS_INSTALL ? "已安装":"未安装";
            });
            $grid->column('','付款状态')->display(function(){
                $var = Order::find($this->order_id);
                return  $var && $var->pay_time   ? "已付款":"未付款";
            });
            $grid->filter(function(Grid\Filter $filter){
                // 禁用id查询框
                $filter->disableIdFilter();
            });
            $grid->disableCreation();
            $grid->last_change_time('上次更换滤芯时间');
            $grid->next_change_time('下次更换滤芯时间');
            $grid->end_time('合同到期时间');
            $grid->continue_time('下次续费时间');
            $grid->installed_at('设备安装时间');
            $grid->created_at('创建时间');
        });
    }
}
